package 剑指offer;

public class p63股票的最大利润 {
    public int maxProfit(int[] prices) {
        if (prices.length==0) return 0;
        //动态规划
        int maxProfit=0;
        int minPrice=Integer.MAX_VALUE;//记录股票最低价
        for (int price:prices) {
//            minPrice=Math.min(minPrice,price);
//            maxProfit=Math.max(maxProfit,price-minPrice);
            if (price<minPrice) {
                minPrice=price;
            }else if(price-minPrice>maxProfit) {
                maxProfit=price-minPrice;
            }
        }
        return maxProfit;

    }

    public int maxProfit1(int[] prices) {
        //暴力求解 两次遍历
        int maxProfit=0;

        for (int i = 0; i <prices.length; i++) {
            for (int j = i+1; j <prices.length; j++) {
                if (prices[j]-prices[i]>maxProfit) {
                    maxProfit=prices[j]-prices[i];
                }
            }
        }
        return maxProfit;
    }
}
